<template>
  <el-dialog
    append-to-body
    destroy-on-close
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" :model="form">
      <el-form-item label="凭证图片">
        <images-upload
          v-if="dialogFormVisible"
          ref="imagesUpload"
          :image="image"
          :limit="3"
          :max="10"
          @onChange="getImages"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { editBill } from '@/api/bill'
  import { editOwnerBill } from '@/api/ownerBill'
  import imagesUpload from '@/components/imagesUpload'
  export default {
    name: 'BillImages',
    components: { imagesUpload },
    data() {
      return {
        type: '',
        form: {
          bill_id: '',
          field_name: 'bill_images',
          field_value: '',
        },
        image: '',
        title: '编辑图片',
        dialogFormVisible: false,
      }
    },
    methods: {
      showEdit(type, row) {
        this.type = type
        this.form.bill_id = row.id
        this.image = row.bill_images
        this.dialogFormVisible = true
      },
      close() {
        this.image = ''
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      getImages(e) {
        this.form.field_value = e.join(',')
      },
      save() {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            this.edit()
          }
        })
      },
      async edit() {
        if (this.type == 'rent') {
          const { data } = await editBill(this.form)
          if (data.is_success) {
            this.$baseMessage('修改成功', 'success', 'vab-hey-message-success')
            this.$emit('fetch-data')
            this.close()
          }
        } else if (this.type == 'owner') {
          const { data } = await editOwnerBill(this.form)
          if (data.is_success) {
            this.$baseMessage('修改成功', 'success', 'vab-hey-message-success')
            this.$emit('fetch-data')
            this.close()
          }
        }
      },
    },
  }
</script>
